Component({
  data: {
    bookInfo: {},
    windowWidth: 0,
    currentTab: 0,
    navScrollLeft: 0,
    cateList:[],
    navData: [{
      text: '出版社',
    },
    {
      text: '作者'
    },
    {
      text: '系列'
    },
    {
      text: '国家'
    }]
  },
  methods: {
    onLoad(options: any) {
      console.log(options)
      const data = options.id
      //获取屏幕宽度
      wx.getSystemInfo({
        success: (res) => {
          console.log(res)
          this.setData({
            windowWidth: res.windowWidth
          })
        },
      })
      this.getCategory(1)
    },
    switchNav(event) {
      var cur = event.currentTarget.dataset.current;
      //每个tab选项宽度占1/5
      var singleNavWidth = this.data.windowWidth / 5;
      //tab选项居中                            
      this.setData({
        navScrollLeft: (cur - 2) * singleNavWidth
      })
      if (this.data.currentTab == cur) {
        return false;
      } else {
        this.setData({
          currentTab: cur
        })
        this.getCategory( cur + 1 )
      }
    },
    switchTab(event) {
      var cur = event.detail.current;
      var singleNavWidth = this.data.windowWidth / 5;
      this.setData({
        currentTab: cur,
        navScrollLeft: (cur - 2) * singleNavWidth
      });
    },
    // 获得分类
    getCategory(data: any) {
      wx.request({
        url: `http://localhost:8888/api/classify/list`,
        method: 'GET',
        data: {type:data},
        header: { 'Content-Type': 'json' },
        success: (res) => {
          console.log(res.data)
          this.setData({cateList:res.data.data})
        },
        fail: (err) => console.error(err)
      });
    },
    //点击分类
    startDetailsOnClick(event){
      console.log(event)
      const data=event.currentTarget.dataset.item.name
      wx.navigateTo({
        url: '../catelog/catelog?name='+ data+'&type='+encodeURIComponent(this.data.currentTab+1),
      })
    }
  }
})